<?php
	class SubjectsController extends AppController{
		
		var $name = "Subjects";
		
		function index(){
			$this->set('subjects', $this->Subject->find('all'));
		}
		
		/**
		 * Method used to get all data from Subjects tables
		 */
		function listSubject(){
			//Get all record
			$this->set('subjects', $this->Subject->find('all'));
		}
		
		/**
		 * Method used to get specified record and display data on input screen
		 */
		function editSubject(){
			$subId = $_POST["id"];
			$this->set('subject', $this->Subject->findAllBysub_id($subId));
			
		}
		
		/**
		 * Method used to display input subject page
		 */
		function inputSubject(){
			$subName = isset($_POST["subName"]) ? $_POST["subName"] : "";
			$subDesript = isset($_POST["subDescript"]) ? $_POST["subDescript"] : "";
			$this->set('subName', $subName);
			$this->set('subDescript', $subDesript);
		}
		
		/**
		 * Method used to display confirm edit/regist page
		 */
		function confirmSubject(){
			$subId = isset($_POST["subId"]) ? $_POST["subId"] : "";
			$subName = $_POST["subName"];
			$subDesript = $_POST["subDescript"];
			$confirmType = $_POST["confirmType"];
			$this->set('confirmType', $confirmType);
			$this->set('subId',$subId);
			$this->set('subName', $subName);
			$this->set('subDescript', $subDesript);
		}
		
		/**
		 * Method used to display regist page
		 */
		function registSubject(){
			$registType = $_GET["type"];
			if($registType == "insert"){
				$this->Subject->create();
				$this->request->data['sub_id'] = null;
				$subName = $_POST["subName"];
				$subDesript = $_POST["subDescript"];
				$this->request->data['sub_name'] = $subName;
				$this->request->data['sub_descript'] = $subDesript;
				if($this->Subject->save($this->request->data)){
					$this->redirect(array('action' => 'listSubject', null));
				}else{
					$this->set('result', 'fail');
				}
			}else{
				$dataNeedToSave = array('Subject.sub_id' => $this->request->data['subId'],
					                'Subject.sub_name' => "'" . $this->request->data['subName'] . "'",
					                'Subject.sub_descript' => "'". $this->request->data['subDescript'] . "'");
				$saveCondition = array('Subject.sub_id' => $this->request->data['subId']);
				if ($this->Subject->updateAll($dataNeedToSave, $saveCondition)) {
					$this->redirect(array('action' => 'listSubject', null));
				} else {
					$this->Session->setFlash('Unable to update schedule.');
				}
			}
		}
		
		function deleteSubject(){
			$id = $_POST['id'];
			if ($this->Subject->deleteAll(array('Subject.sub_id' => $id), false)) {
				$this->Session->setFlash('The schedule with id: ' . $id . ' has been deleted.');
				$this->redirect(array('action' => 'listSubject', $id));
			}else{
				echo "fail";die;
			}
		}
	}
?>